<?

require_once('config.php');
require_once('functions/general.php');

define('TPL_THREAD_FORM', 'discussion_thread_form.tpl');
define('TPL_SUBBACK', "discussion_subback.tpl");
define('TPL_OVERVIEW', "discussion.tpl");
define('TPL_THREAD', "discussion_thread.tpl");

class Discussion {
    private $smarty;
    private $sql_discuss;

    public function __construct($smarty, $mysql_link) {
        $this->smarty = $smarty;
        $this->sql_discuss = new Sql_discussion($mysql_link);
    }

    public function show_thread($id) {
        if (!is_numeric($id) 
                or !$this->sql_discuss->thread_exists($id)) {
            $this->smarty->assign('error_thread_not_exist', true);
            return TPL_THREAD;
        }

        $thread = $this->sql_discuss->thread_with_posts($id);
        $this->smarty->assign('thread', $thread);


        return TPL_THREAD;
    }

    public function show_thread_form() {
        return TPL_THREAD_FORM;
    }

    public function show_subback() {
        $this->smarty->assign('threads', 
                $this->sql_discuss->get_threads());
        return TPL_SUBBACK;
    }

    public function show_overview() {
        global $CONFIG;

        $detailed_cnt = $CONFIG['discussion_threads_detailed'];
        $simple_cnt   = $CONFIG['discussion_threads_simple'];
        $detailed_threads 
            = $this->sql_discuss->detailed_overview($detailed_cnt);

        $simple_threads
            =  $this->sql_discuss
            ->simple_overview($simple_cnt, $detailed_cnt);

        $this->smarty->assign('detailed_threads', $detailed_threads);
        $this->smarty->assign('simple_threads', $simple_threads);

        return TPL_OVERVIEW;
    }

    public function add_new_thread($text, $title, $sage, $username) {
        $smarty     = $this->smarty;
        $discuss = $this->sql_discuss;

        $sage = $sage ? true : false;

        if (!$text) {
            $smarty->assign('error_content_missing', true);
            $smarty->assign('thread_title', $title);
            return TPL_THREAD_FORM;
        }

        if (!$title) {
            $smarty->assign('error_title_missing', true);
            $smarty->assign('thread_text', $text);
            return TPL_THREAD_FORM;
        }
        else {
            // All good. Inserting thread.
            $thread = $discuss->insert_thread($title);

            $discuss->insert_post($thread, $text, $sage, $username);
            $smarty->assign('success_insert_thread', true);
            return $this->show_overview();
        }
    }

    public function add_new_reply($id, $text, $sage, $logged_in) {
        global $CONFIG;
        $poster_name = $logged_in 
            ? $_SESSION['user'] : $CONFIG['discussion_anonymous'];
        $thread_exists = $this->sql_discuss->thread_exists($id);

        if (!is_numeric($id) or !$thread_exists) {
            $this->smarty->assign('error_thread_not_exist', 1);
            return $this->show_overview();
        }

        if (!$text) {
            $this->smarty->assign('error_content_missing', true);
            return $this->show_thread($id);

        }

        # All good, adding reply.
        $this->sql_discuss->insert_post(
                $id, $text, $sage, $poster_name);
        $this->smarty->assign('success_insert_reply', true);
        return $this->show_overview();
    }


}
